package xyz.o0o0o.demo.AuthorizationServerDemo2.entity;

import java.util.Collection;
import java.util.Map;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

public class OAuthClientDetails {
	private String resource_ids;
	private String client_secret;
	private String scope;
	private String client_id;
	private String authorized_grant_types;
	private String web_server_redirect_uri;
	private String authorities;
	private int access_token_validity;
	private int refresh_token_validity;
	private String additional_information;
	private String autoapprove;

	public OAuthClientDetails() {
	}

	public OAuthClientDetails(ClientDetails clientDetails) {
		this.resource_ids = "";
		if (clientDetails.getResourceIds() != null) {
			for (String id : clientDetails.getResourceIds()) {
				this.resource_ids += id + ",";
			}
			this.resource_ids = this.resource_ids.substring(0, this.resource_ids.length() - 1 - 1);
		}

		this.client_secret = clientDetails.getClientSecret();

		this.scope = "";
		if (clientDetails.getScope() != null) {
			for (String scope : clientDetails.getScope()) {
				this.scope += scope + ",";
			}
			this.scope = this.scope.substring(0, this.scope.length() - 1 - 1);
		}

		this.client_id = clientDetails.getClientId();

		this.authorized_grant_types = "";
		if (clientDetails.getAuthorizedGrantTypes() != null) {
			for (String types : clientDetails.getAuthorizedGrantTypes()) {
				this.authorized_grant_types += types + ",";
			}
			this.authorized_grant_types = this.authorized_grant_types.substring(0,
					this.authorized_grant_types.length() - 1 - 1);
		}

		this.web_server_redirect_uri = "";
		if (clientDetails.getRegisteredRedirectUri() != null) {
			for (String url : clientDetails.getRegisteredRedirectUri()) {
				this.web_server_redirect_uri += url + ",";
			}
			this.web_server_redirect_uri = this.web_server_redirect_uri.substring(0,
					this.web_server_redirect_uri.length() - 1 - 1);
		}

		this.authorities = "";
		if (clientDetails.getAuthorities() != null) {
			Collection<GrantedAuthority> authoritiesTemp = clientDetails.getAuthorities();
			for (GrantedAuthority grantedAuthority : authoritiesTemp) {
				this.authorities += grantedAuthority.getAuthority() + ",";
			}
			this.authorities = this.authorities.substring(0, this.authorities.length() - 1 - 1);
		}

		this.access_token_validity = 1;

		this.refresh_token_validity = 1;

		this.additional_information = "";
		if (clientDetails.getAdditionalInformation() != null) {
			Map<String, Object> informations = clientDetails.getAdditionalInformation();
			for (String key : informations.keySet()) {
				this.additional_information += informations.get(key) + ",";
			}
			this.additional_information = this.additional_information.substring(0,
					this.additional_information.length() - 1 - 1);
		}

		this.autoapprove = "false";

	}

	public String getResource_ids() {
		return resource_ids;
	}

	public void setResource_ids(String resource_ids) {
		this.resource_ids = resource_ids;
	}

	public String getClient_secret() {
		return client_secret;
	}

	public void setClient_secret(String client_secret) {
		this.client_secret = client_secret;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getClient_id() {
		return client_id;
	}

	public void setClient_id(String client_id) {
		this.client_id = client_id;
	}

	public String getAuthorized_grant_types() {
		return authorized_grant_types;
	}

	public void setAuthorized_grant_types(String authorized_grant_types) {
		this.authorized_grant_types = authorized_grant_types;
	}

	public String getWeb_server_redirect_uri() {
		return web_server_redirect_uri;
	}

	public void setWeb_server_redirect_uri(String web_server_redirect_uri) {
		this.web_server_redirect_uri = web_server_redirect_uri;
	}

	public String getAuthorities() {
		return authorities;
	}

	public void setAuthorities(String authorities) {
		this.authorities = authorities;
	}

	public int getAccess_token_validity() {
		return access_token_validity;
	}

	public void setAccess_token_validity(int access_token_validity) {
		this.access_token_validity = access_token_validity;
	}

	public int getRefresh_token_validity() {
		return refresh_token_validity;
	}

	public void setRefresh_token_validity(int refresh_token_validity) {
		this.refresh_token_validity = refresh_token_validity;
	}

	public String getAdditional_information() {
		return additional_information;
	}

	public void setAdditional_information(String additional_information) {
		this.additional_information = additional_information;
	}

	public String getAutoapprove() {
		return autoapprove;
	}

	public void setAutoapprove(String autoapprove) {
		this.autoapprove = autoapprove;
	}

}
